package com.example.shop.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.shop.modules.sys.service.IUserService;
import com.example.shop.persistence.entity.User;
import com.example.shop.utils.JSONResult;
import com.example.shop.utils.MPPageFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jason
 * @since 2019-12-17
 */
@Api(tags = "用户表相关接口")
@RestController
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public JSONResult insertUser(User user){
        return JSONResult.renderSuccess(userService.save(user));
    }

    /**
     * 检查用户名和邮箱是否被占用
     * @param username
     * @param email
     * @return num  0：没有   1用户名被占用  2邮箱被占用
     */
    @ApiOperation(value = "检查用户名和邮箱是否被占用", notes = "返回 0：没有   1用户名被占用  2邮箱被占用 3手机号被占用")
    @PostMapping("/check")
    public JSONResult checkUser(String username, String email, String phone){
        Integer num = 0;
        if(!StringUtils.isEmpty(username)){
            QueryWrapper ew = new QueryWrapper();
            ew.eq("username",username);
            User user = userService.getOne(ew,false);
            if(!StringUtils.isEmpty(user)){
                num = 1;
            }
        }
        if(!StringUtils.isEmpty(email)){
            QueryWrapper ew = new QueryWrapper();
            ew.eq("email",email);
            User user = userService.getOne(ew,false);
            if(!StringUtils.isEmpty(user)){
                num = 2;
            }
        }
        if(!StringUtils.isEmpty(phone)){
            QueryWrapper ew = new QueryWrapper();
            ew.eq("phone",phone);
            User user = userService.getOne(ew,false);
            if(!StringUtils.isEmpty(user)){
                num = 3;
            }
        }
        return JSONResult.renderSuccess(num);
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/update")
    public JSONResult updateUser(User user){
        return JSONResult.renderSuccess(userService.updateById(user));
    }


    @ApiOperation(value = "用户登录",notes = "用户名或手机号登陆，密码必填。登录成功返回用户信息 ")
    @PostMapping("/login")
    public JSONResult loginUser(String username, String phone, String password){
        return userService.loginUser(username, phone, password);
    }

    @ApiOperation(value = "查询用户信息")
    @GetMapping("/{id}")
    public JSONResult getUserInfo(Integer id){
        return JSONResult.renderSuccess(userService.getById(id));
    }

}

